package com.view;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CursorTreeAdapter;
import android.widget.TextView;

import com.usercarshow.R;
import com.util.CarDBHelper;

public class ExhibitBrand extends ExpandableListActivity {
	// "主页" 按钮
	private Button home;
	int countryNameIndex;
	private MyCursorTreeAdapter myCursorTreeAdapter;
	// 声明操作数据库的对象
	private CarDBHelper carDBHelper;
	// 当前类的一个静态对象
	static ExhibitBrand exhibitBrand;
	private Cursor countryCursor;
	// 联系人组上groupName字段索引
	private static final int countryName_index = 1;
	private static final int brand_index = 1;
	private static final int name_index = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		System.out.println("-----ExhibitBrand onCreate----");
		super.onCreate(savedInstanceState);
		ExhibitBrand.exhibitBrand = this;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
		setContentView(R.layout.exhibit_brand);
		// 注册菜单
		registerForContextMenu(getExpandableListView());
		// 拖动时避免出现黑色
		getExpandableListView().setCacheColorHint(0);
		// 去掉每页下面的黑线
		getExpandableListView().setDivider(null);
		carDBHelper = new CarDBHelper(this);
		carDBHelper.open();
		initMyAdapter();

	}

	/**
	 * 给适配器赋值，刷新界面的时候也会用到
	 */
	public void initMyAdapter() {
		countryCursor = carDBHelper.queryAllCountrys();
		startManagingCursor(countryCursor);
		// 获得组名列的索引
		countryNameIndex = countryCursor.getColumnIndexOrThrow("country_name");
		// 设置适配器
		myCursorTreeAdapter = new MyCursorTreeAdapter(countryCursor, this, true);
		setListAdapter(myCursorTreeAdapter);
	}

	private class MyCursorTreeAdapter extends CursorTreeAdapter {

		public MyCursorTreeAdapter(Cursor cursor, Context context,
				boolean autoRequery) {
			super(cursor, context, autoRequery);
		}
		
		@Override
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			// 通过字段索引得到当前的组名
			String groupName = groupCursor.getString(countryName_index);
			Cursor childCursor = carDBHelper.getcarsByCountryName(groupName);
			startManagingCursor(childCursor);
			return childCursor;
		}

		@Override
		protected View newGroupView(Context context, Cursor cursor,
				boolean isExpanded, ViewGroup parent) {
			LayoutInflater inflate = LayoutInflater.from(ExhibitBrand.this);
			View view = inflate.inflate(R.layout.grouplayout, null);
			bindGroupView(view, context, cursor, isExpanded);
			return view;
		}

		@Override
		protected void bindGroupView(View view, Context context, Cursor cursor,
				boolean isExpanded) {
			// 通过findViewById()方法实例化TextView
			TextView countryName = (TextView) view.findViewById(R.id.groupName);
			String country = cursor.getString(countryName_index);
			countryName.setText(country);

			TextView countryCount = (TextView) view
					.findViewById(R.id.groupCount);
			int count = carDBHelper.getCountCarsByCountryName(country);
			countryCount.setText("[" + count + "]");

		}

		@Override
		protected View newChildView(Context context, Cursor cursor,
				boolean isLastChild, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(ExhibitBrand.this);
			// 目的都是把xml表述的layout转化为View
			View view = inflater.inflate(R.layout.childlayout, null);
			bindChildView(view, context, cursor, isLastChild);
			return view;
		}

		@Override
		protected void bindChildView(View view, Context context, Cursor cursor,
				boolean isLastChild) {
			System.out.println("---------bindChildView-----");
			TextView name = (TextView) view.findViewById(R.id.name);
			name.setText(cursor.getString(name_index));

			TextView description = (TextView) view
					.findViewById(R.id.description);
			description.setTextKeepState(cursor.getString(brand_index));
		}

	}

	// Activity销毁时的回调函数
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (carDBHelper != null) {
			carDBHelper.close();
			carDBHelper = null;

		}
	}

}
